<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>反审编辑</title>
<!-- import css and js -->
<%@ include file="/common/metaInput.jsp"%>
<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script src="${ctx}/js/jsp/date.js" type="text/javascript"></script>
<script type="text/javascript">
var type='${type}';
var basic_Url = "${ctx}/logic/checkback";
var addressGrid;
var print=false;
jQuery(document).ready(function() {
	//submit事件
	$("#inputForm").validate({
		onsubmit : true,
		/*执行ajaxsubmit  */
		submitHandler : function(editform) {

			try {
				url = basic_Url + "/save.do";
				var api = frameElement.api;
				var W = api.opener;
				var obj={'zd001CheckCode':$('#zd001CheckCode').val(),'zd004WhName':$('#zd004WhName').val(),'zd009Date':$('#zd009Date').val(),'outDate':$('#outDate').val(),'zd002BackCode':$('#zd002BackCode').val(),'zd006Aegnt':$('#zd006Aegnt').val(),'aegnt':$('#aegnt').val()};
				getReturnMessageThree(url, "inputForm", W.searchAP,"",obj,addressGrid.getData(),false,"cbotherout");
			} catch (e) {
				alert(e);
			}
		}
	});

	if ('${cb.zd011Stauts}' == ""){
		$("#save1").hide();
		$("#save2").hide();
		$("#save3").hide();
		$("#tr1").hide();
		$("#print1").hide();
		$("#print2").hide();
	}else if ('${cb.zd011Stauts}' == "SAVE"){
		$("#save").hide();
		$("#save2").hide();
		$("#save3").hide();
        if($("#zd006Aegnt").val()!="${user.realName}"){
			$("#save1").hide();
			$("input[type!='hidden']").attr("disabled",true);//禁用SELECT标签
			$("select").attr("disabled",true);//禁用SELECT标签
			$("textarea").attr("disabled",true);//禁用TEXTAREA标签
           }  
	}else if ('${cb.zd011Stauts}' == "COMMIT"){
		$("#save").hide();
		$("#save1").hide();
		$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
		$("select").attr("disabled","disabled");//禁用SELECT标签
		$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
	}else if ('${cb.zd011Stauts}' == "AUDIT"){
		$("#save").hide();
		$("#save1").hide();
		$("#save2").hide();
		$("#save3").hide();
		$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
		$("select").attr("disabled","disabled");//禁用SELECT标签
		$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
	}
	//创建JQGRID表格
	var column1 = [      
	               	{ display: 'id', name: 'id',type: 'string',hide:true},
	               	{ display: '物料id', name: 'zd002MaterialsId',width:1,hide:true},
	               	{ display: 'piCode', name: 'zd025PiCode',width:1,hide:true},
	               	{ display: '<font color="gray">产品编码</font>', name: 'zd003MaterialsCode', width: 120},
	               	{ display: '<font color="gray">产品名称</font>', name: 'zd004MaterialsName',width: 120},
	               	{ display: '<font color="gray">产品型号</font>', name: 'zd006MaterialsModel', width: 100},
	           		{ display: '<font color="gray">产品尺寸</font>', name: 'zd005MaterialsSpec', width: 160},
	           		{ display: '<font color="gray">产品颜色</font>', name: 'zd017MaterialsColor', width: 100},
	           		{ display: '<font color="gray">产品材质</font>', name: 'zd018MaterialsQuality', width: 100},
	           		{ display: '<font color="gray">基本单位</font>', name: 'zd008Unit' ,width: 100},
	           		{ display: '<font color="gray">库存数量</font>', name: 'zd019WhQuanlity',width: 100 },
	           		{ display: '<font color="gray">出库数量</font>', name: 'zd020WareOutQuanlity',width: 100 },
	           		{ display: '<font color="gray">已反审数量</font>', name: 'zd024HsBackQuantity',width: 100 },
	           		{ display: '反审数量<font color="red">*</font>', name: 'zd023BackQuanlity',editor : {type :'float'},width: 150},
	              	{ display: '单价<font color="red">*</font>',name: 'zd009Price',hide:true}
	               ];
	var column2 = [      
	               	{ display: 'id', name: 'id',type: 'string',hide:true},
	               	{ display: '物料id', name: 'zd002MaterialsId',width:1,hide:true},
	               	{ display: 'piCode', name: 'zd025PiCode',width:1,hide:true},
	               	{ display: '<font color="gray">产品编码</font>', name: 'zd003MaterialsCode', width: 120},
	               	{ display: '<font color="gray">产品名称</font>', name: 'zd004MaterialsName',width: 120},
	               	{ display: '<font color="gray">产品型号</font>', name: 'zd006MaterialsModel', width: 100},
	           		{ display: '<font color="gray">产品尺寸</font>', name: 'zd005MaterialsSpec', width: 160},
	           		{ display: '<font color="gray">产品颜色</font>', name: 'zd017MaterialsColor', width: 100},
	           		{ display: '<font color="gray">产品材质</font>', name: 'zd018MaterialsQuality', width: 100},
	           		{ display: '<font color="gray">基本单位</font>', name: 'zd008Unit' ,width: 100},
	           		{ display: '<font color="gray">库存数量</font>', name: 'zd019WhQuanlity',width: 100 },
	           		{ display: '<font color="gray">出库数量</font>', name: 'zd020WareOutQuanlity',width: 100 },
	           		{ display: '<font color="gray">已反审数量</font>', name: 'zd024HsBackQuantity',width: 100 },
	           		{ display: '反审数量<font color="red">*</font>', name: 'zd023BackQuanlity',width: 150},
	              	{ display: '单价<font color="red">*</font>',name: 'zd009Price',hide:true}
	               ];
	 var column3 = null;
	 if ('${cb.zd011Stauts}' == ''||'${cb.zd011Stauts}'=='RETURN'||'${cb.zd011Stauts}'=='SAVE'){
		 column3 = column1;
	 }else {
		 column3 = column2;
	 }
	 addressGrid = $("#dataGrid1").ligerGrid({
   		 columns: column3,
   	                    enabledEdit : true,
   	                    usePager: true,
   	      				width : '99%',
   	      				pageSize:1000,
   	      				pageSizeOptions:[1000],
   	      				parms:[{ name : 'id' , value : '${cb.id}'},{name:'type',value:'${type}'}],
         		        	url : basic_Url+"/gridCBitems.do",
   	      				rownumbers : false,
   	      				checkbox : true,
   	                    height : '90%',
   		toolbar : {
   			items : [{
   				text : '删除',
   				click : function() {
   					if ('${cb.zd011Stauts}' == ''||'${cb.zd011Stauts}'=='RETURN'||'${cb.zd011Stauts}'=='SAVE'){
   					addressGrid.deleteSelectedRow();
   					}
   				},
   				img : '${ctx}/js/ligerUI/skins/icons/delete.gif'
   			}]
   		}});
     
	   if($("#id").val()!=''){
	    	 $.ajax({
	    		 	url:basic_Url+'/chkeckObjectInfo.do',
	    		 	global: false,
	    		 	type: 'POST',
	    		 	dataType:'json',
	    		 	data : ({
	    		 		id:'${cb.zd002BackId}',
	    		 		type : '${type}'
	    		 	}),success: function(data){
	    		 		//出库日期
						$("#outDate").val(formatDate(new Date(data.outdate),"yyyy-MM-dd"));
						//原始单号
						//$("#oriCode").val(data.zd002OriCode);
						//反审对象经办人
						$("#aegnt").val(data.aegnt);
	    		 	}
	    	 });
	     }
});

function submitForm(param) {
 	$("#zd011Stauts").val(param);
 	$("#addressInfos").val(JSON.stringify($("#dataGrid1").ligerGetGridManager().getData()));
	var data = $("#dataGrid1").ligerGetGridManager().getData();
	if(data.length==0){
		$.dialog.alert("请选择反审单");
   	 	return false;
	}
	var ba = true;
	if(data !=null){ 
		for (var i = 0;i < data.length;i++){
			var row = data[i]; 
			if(row.zd023BackQuanlity>row.zd020WareOutQuanlity){
				$.dialog.alert("您第"+(i+1)+"行的“反审数量”大于“已出库数量”无法反审");
	           	 ba=false;
	           	 return false;
			}
			if(row.zd023BackQuanlity==undefined||isNaN(row.zd023BackQuanlity)){
				$.dialog.alert("您第"+(i+1)+"行反审数目不能为空");
		   	 	return false;
			}
		}
	}
	//设置ajax全局同步
	jQuery.ajaxSettings.async = false;
	if(ba){
		$("#submitFlag").val(param);
		if(param=='COMMIT'){
			print=true;
		}
		$("#inputForm").submit();
	}
}

function close(){
	var api = frameElement.api;
	var W = api.opener;
	CloseDialog();
	W.searchAP();
}

function PrintView(type){
	var data = addressGrid.getData();//获取单据要打印物料
	var isprint=false;
	if(type==2){
		isprint=true;
	}
	window.parent.parent.loadPrint(obj,data,'prostorageout',isprint);
}


function getBackInfo(){
	getDG().dialog(
			{
				title : '选择反审单据',
				width : 650,
				height : 350,
				content : 'url:' + sys_context
						+ '/public/windowalert/checkback.do?type='+'${type}',    
				lock : true,
				max : true,
				min : true,
				ok : function() {
					var rows = this.content.getSelected();
					if(rows.length!=1){
						$.dialog.alert("每次必须是选择一笔数据");
						return false;
					}
					$(rows).each(function(){
							//反审对象Id
							$("#zd002BackId").val(this.zd002BackId);
							//反审对象编码
							$("#zd002BackCode").val(this.zd002BackCode);
							// PI单号
							$("#zd003PiCode").val(this.zd003PiCode);
							//仓库ID
							$("#zd004Whid").val(this.zd004Whid);
							// 仓库编码
							$("#zd004WhCode").val(this.zd004WhCode);
							//仓库名称
							$("#zd004WhName").val(this.zd004WhName);
							//客户编码
							$("#zd007CustomerCode").val(this.zd007CustomerCode);
							//客户ID
							$("#zd007CustomerId").val(this.zd007CustomerId);
							//客户名称
							$("#zd007CustomerName").val(this.zd007CustomerName);
							//出库日期
							$("#outDate").val(formatDate(new Date(this.zd009Date),"yyyy-MM-dd"));
							//原始单号
							//$("#oriCode").val(this.zd001CheckCode);
							//反审对象经办人
							$("#aegnt").val(this.zd006Aegnt);
							var url=basic_Url+"/griditems.do?id="+this.zd002BackId+"&type="+'${type}';
							multipleSearch("#dataGrid1",true,"",url);
					});
					this.content.Close();
					return false;
				},
				cancelVal : '关闭',
				cancel : true
			}).zindex();
	
}
function check(){
	$.ajax({
		url : '${ctx}/logic/checkback/updateState.do',
		global :false,
		type: 'POST',
		dataType:'json',
		data:({id: $('#id').val()}),
		success: function(msg){
			 if(msg.msg=="SUCCESS"){
	    		  $.dialog.alert("驳回成功",function(){close();});
	    	  }
	    	  if(msg.msg=="ERROR"){
	    		  $.dialog.alert("操作失败",function(){close();});
	    	  }
		}
	});
}

</script>

</head>

<body>
	<form id="inputForm" action="" method="post">
	<input id='id' name='id' value='${cb.id }' type="hidden"/>
	<input id='zd002BackId' name='zd002BackId' value='${cb.zd002BackId }' type="hidden"/>
	<input id='zd005Batch' name='zd005Batch' value='${cb.zd005Batch}' type="hidden"/>
	<input id='zd008Type' name='zd008Type' value='${type}' type="hidden"/>
	<input  id="zd011Stauts" name="zd011Stauts" value='${cb.zd011Stauts}' type="hidden"/>
	<input  id="bizVersion" name="bizVersion" value='${cb.bizVersion}' type="hidden"/>
	<input  id="addressInfos" name="addressInfos" type="hidden"/>
		<!-- 用于标识提交按钮 -->
		<input type="hidden" id="submitFlag" value="" />
		<div class="pageContent">
			<div class="panel">
				<table class="TableBlock" align="center">
					<tr id="tr1">
					 	<td>反审单号:</td>
						<td ><input class="inp_width_98p"  type="text" id="zd001CheckCode" name="zd001OutCode" readonly="readonly" value="${cb.zd001CheckCode}" /></td>
						<td colspan="2"></td>
					</tr>
					<tr>
						<td>反审对象单号:</td>
						<td><input class="required"  name="zd002BackCode" id="zd002BackCode" readonly="readonly" value="${cb.zd002BackCode}"/>
						<input value="导入" type="button" onclick="getBackInfo()" /> </td>
						<td>反审对象经办人:</td>
						<td>
							<input type="text" readonly="readonly" id="aegnt" name="aegnt"/>
						</td>
					</tr>
					<tr>
						<td>出库仓库:</td>
						<td>
							<input type="hidden" readonly="readonly" id="zd004Whid" name="zd004Whid" value="${cb.zd004Whid }"/>
							<input type="hidden" readonly="readonly" id="zd004WhCode" name="zd004WhCode" value="${cb.zd004WhCode }"/>
   		             		<input type="text" readonly="readonly" id="zd004WhName" name="zd004WhName" value="${cb.zd004WhName }"/>
						</td>
					 	<td>出库时间:</td>
						<td> <input type="text" id="outDate"   class="inp_width_98p" readonly="readonly"/>
						</td>
					</tr>
					<tr>
					 	<td>客户编码:</td>
						<td><input id="zd007CustomerCode" name="zd007CustomerCode" readonly="readonly" class="inp_width_98p required" value="${cb.zd007CustomerCode}"></input>
						<input type="hidden" id="zd007CustomerId" name="zd007CustomerId" value="${cb.zd007CustomerId}"></input>
						</td>
					 	<td>客户名称:</td>
						<td><input id="zd007CustomerName" name="zd007CustomerName" readonly="readonly" class="inp_width_98p required" value="${cb.zd007CustomerName}"></input>
						</td>
					</tr>
					<tr>
					 	<td>PI单号:</td>
						<td>
							<input id="zd003PiCode" name="zd003PiCode" readonly="readonly" class="inp_width_98p" value="${cb.zd003PiCode}" />
						</td>
						<td>反审日期:</td>
						<td >
							<input id="zd009Date" name="zd009Date" class="inp_width_98p required" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" value="<fmt:formatDate value="${cb.zd009Date}" pattern="yyyy-MM-dd" type="both" />" />
							 <c:if test="${cb.zd009Date == null}">
									<script>
										var date = new Date();
										$("#zd009Date").val(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());
									</script>
							 </c:if>
						</td>
					</tr>
					<tr>
						<td>反审人:</td>
						<td >
						    <input type="text" id="zd006Aegnt" name='zd006Aegnt' value='${cb.zd006Aegnt }'  class="inp_width_98p" readonly="readonly"/>
					 		<c:if test='${cb.zd006Aegnt==null }' >
					 			<script>
					 				$("#zd006Aegnt").val('${user.realName}');
					 			</script>
					 		</c:if>
					  </td>
				      <td>备注:</td>
					  <td>
					  	<input id="zd010Remark" name="zd010Remark" class="inp_width_98p" value="${cb.zd010Remark}" /></td>
					</tr>
					<tr>
						<td colspan="4" style="margin-bottom: 10px;"><div
								id="dataGrid1"></div></td>
					</tr>
				</table>
			</div>
			<div class="formBar">
				<ul>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm('SAVE')">保存</button></div></div></li>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save1" onclick="submitForm('COMMIT')">提交</button></div></div></li>
		        <webres:displaycontroller elementId="LOGIC_SALESOUT_CK_SP" description="其他出库反审">
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save2" onclick="submitForm('AUDIT')">审核通过</button></div></div></li>
			    	<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save3" onclick="check()">驳回</button></div></div></li>
			    </webres:displaycontroller>

					<!-- <li id='print1'><div class="buttonActive"><div class="buttonContent"><button type="button" id="forPrint" onclick="PrintView(1);">打印预览</button></div></div></li>
		      	<li id='print2'><div class="buttonActive"><div class="buttonContent"><button type="button" id="forPrint" onclick="PrintView(2);">打印</button></div></div></li> -->
		      		 <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
				</ul>
			</div>
		</div>
	</form>
</body>
</html>
